package com.example.smartretaildemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import com.example.smartretaildemo.databinding.ActivityMainBinding;
import com.example.smartretaildemo.module.classification.fragment.ClassificationFragment;
import com.example.smartretaildemo.module.personal.PersonalFragment;
import com.example.smartretaildemo.module.recommend.RecommendFragment;
import com.example.smartretaildemo.module.shoppingcart.fragment.ShoppingCartFragment;
import com.example.smartretaildemo.module.classification.activity.ClaChooseActivity;

import java.util.HashSet;
import java.util.Set;

public class MainActivity extends AppCompatActivity {
//  底边栏tab
    private String[] tabs = new String[]{"精彩推荐", "分类详细", "购物车", "个人中心"};
    private Class[] mFragmentClasses = new Class[]{RecommendFragment.class,
            ClassificationFragment.class, ShoppingCartFragment.class, PersonalFragment.class};
    private int[] selectorImg = new int[]{R.drawable.rank,R.drawable.apps, R.drawable.cart,
            R.drawable.profile_light};
    private int currentTab;
//    ViewBinding
    ActivityMainBinding mainBinding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        自定义标题栏
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        mainBinding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(mainBinding.getRoot());
//        默认tab-默认为详细页，结算跳转为购物车
        currentTab = getIntent().getIntExtra("CurrentTab", 1);
//        缓存读取分类列表
        SharedPreferences sp = getSharedPreferences("calsList01", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        Set<String> claList = new HashSet<>();
        Set<String> list=new HashSet<>();
//        默认为类型1（纸巾）
        list.add("1");
        claList = sp.getStringSet("cals", list);
        editor.putStringSet("cals", claList);
        editor.commit();
        initTab();
    }

//    初始化tab
    private void initTab() {
        mainBinding.tab.setup(this, getSupportFragmentManager(), R.id.fragment_content);
        for (int i = 0; i < 4; i++) {
            mainBinding.tab.addTab(mainBinding.tab.newTabSpec(tabs[i]).setIndicator(getTabView(i)), mFragmentClasses[i], null);
        }
//        设置默认tab
        mainBinding.tab.setCurrentTab(currentTab);
        initTabIndex();
//        点击tab
        mainBinding.tab.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String s) {
                TabChange(s);
            }
        });
    }

//    根据所选tab显示标题栏
    private void initTabIndex() {
        if (mainBinding.tab.getCurrentTab() == 1) {
            getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.cla_title_bar);
            initTitleBar();
        } else if (mainBinding.tab.getCurrentTab() == 2) {
            getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.shop_title_bar);
        } else {
            getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar);
        }
    }

//    初始化自定义标题栏
    private void initTitleBar() {
        View viewClaTitleBarSearch = findViewById(R.id.viewClaTitleBarSearch);
        viewClaTitleBarSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            }
        });
        ImageView imgClaTitleBarSearch = findViewById(R.id.imgClaTitleBarSearch);
        imgClaTitleBarSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            }
        });
        TextView tvClaTitleBarText = findViewById(R.id.tvClaTitleBarText);
        tvClaTitleBarText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            }
        });
        TextView tvClaTitleBarCla = findViewById(R.id.tvClaTitleBarCla);
//        点击筛选跳转
        tvClaTitleBarCla.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, ClaChooseActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

//    底部所选导航栏
    private void TabChange(String s) {
        int tabIndex = mainBinding.tab.getCurrentTab();
        for (int i = 0; i < 4; i++) {
            View view =  mainBinding.tab.getTabWidget().getChildAt(i);
            ImageView ivTab=view.findViewById(R.id.tab_image);
            if (i == tabIndex) {
                ivTab.setColorFilter(Color.GREEN);
            } else {
                ivTab.setColorFilter(Color.BLACK);
            }
            initTabIndex();
        }
    }

//    tab的view对象
    private View getTabView(int index) {
        View inflate = LayoutInflater.from(this).inflate(R.layout.item_tab, null);
        ImageView tabImage = inflate.findViewById(R.id.tab_image);
        TextView tabTitle = inflate.findViewById(R.id.tab_title);
        tabImage.setImageResource(selectorImg[index]); // 通过selector来控制图片的改变
        tabTitle.setText(tabs[index]);// 通过selector来控制文字颜色的改变
        return inflate;
    }
}